﻿/********************************************************************************
* Author:	Christos Polydorou	(xristos.polydoroy@gmail.com)					*
* Author:	Aris Michail		(aris.michail@gmail.com)						*
* Date:		22/9/2011															*
********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace WebApplication
{

    public partial class PropertySearch : System.Web.UI.Page
    {
        DataClassesDataContext dc = new DataClassesDataContext();

        public void MessageBox(String message)
        {

            String script = @"<script type=""text/JavaScript"">alert('" + message + "')</script>";

            Response.Write(script);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myalert", script);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Session["isValid"].ToString().Equals("true"))
                    Response.Redirect("login.aspx");
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                Response.Redirect("login.aspx");
            }


            if (this.DropNomos.SelectedValue.Equals(""))
            {
                this.DropNomos.DataSource = from nomos in dc.Nomois
                                            orderby nomos.Onoma
                                            select new { Onoma = nomos.Onoma };

                this.DropNomos.DataTextField = "Onoma";
                this.DropNomos.DataValueField = "Onoma";
                this.DropNomos.DataBind();
                this.DropNomos.Items.Insert(0, new ListItem("Όλοι οι νομοί", "asdf"));

                this.DropDownList1.Items.Insert(0, new ListItem("Όλοι οι δήμοι", "fasa"));

            }
            else
            {
                if (this.DropDownList1.SelectedValue.Equals(""))
                {
                    this.DropDownList1.DataSource = from dimos in dc.Dimois
                                                    where dimos.Nomos == this.DropNomos.SelectedValue
                                                    orderby dimos.Onoma
                                                    select new { Onoma = dimos.Onoma };
                    this.DropDownList1.DataTextField = "Onoma";
                    this.DropDownList1.DataValueField = "Onoma";
                    this.DropDownList1.DataBind();
                    this.DropDownList1.Items.Insert(0, new ListItem("Όλοι οι δήμοι", "fasa"));
                }
            }
        }

        protected void populate()
        {
            List<Property> results = new List<Property>();
            IQueryable<Property> matches = dc.Properties;

            long l = 0;

            // Add where
            if (this.TextBox3.Text.Trim().Length > 0)
            {
                try
                {
                    l = long.Parse(this.TextBox3.Text.Trim());
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    this.TextBox3.Text = "";
                    HttpContext.Current.Session["message"] = @"Συμληρώστε το πεδίο ""Αριθμός ΟΣΔΕ"" μόνο με αριθμούς";
                }

                matches = matches.Where(a => a.OSDENumber == l);
            }
            if (this.TextBox4.Text.Trim().Length > 0)
            {
                try
                {
                    l = long.Parse(this.TextBox4.Text.Trim());
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    this.TextBox4.Text = "";
                    HttpContext.Current.Session["message"] = @"Συμληρώστε το πεδίο ""Αριθμός Τεμαχίου Αναδασμού"" μόνο με αριθμούς";
                }
                matches = matches.Where(a => a.AnadasmosNumber == l);
            }
            if (this.TextBox5.Text.Trim().Length > 0)
                matches = matches.Where(a => a.OwnerName.StartsWith(this.TextBox5.Text.Trim()));
            if (this.TextBox6.Text.Trim().Length > 0)
                matches = matches.Where(a => a.ProtocolNumber == this.TextBox6.Text.Trim());
            if ((!this.DropNomos.SelectedValue.Equals("asdf")) && (!this.DropNomos.SelectedValue.Equals("")))
                matches = matches.Where(a => a.PropertyNomos == this.DropNomos.SelectedItem.Value);
            if ((!this.DropDownList1.SelectedValue.Equals("fasa")) && (!this.DropDownList1.SelectedValue.Equals("")))
                matches = matches.Where(a => a.PropertyDimos == this.DropDownList1.SelectedItem.Value);


            var query1 = from p in matches
                         select p;
            GridView1.DataSource = query1;
            GridView1.DataBind();

            if (this.GridView1.Rows.Count != 0)
                this.Button2.Visible = true;
            else
                this.Button2.Visible = false;


            GridView2.DataSource = query1;
            GridView2.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
        }
        protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
        {
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.color='DodgerBlue';this.style.cursor='pointer';";
                e.Row.Attributes["onmouseout"] = "this.style.color='Black';";
                e.Row.Attributes["onclick"] = "window.location.href=('propertyupdate.aspx?property=" + e.Row.Cells[0].Text + "')";
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            GridViewExportUtil.Export("SearchResults.xls", this.GridView2);
        }

        protected void DropDownList1_Populate()
        {
            this.DropDownList1.DataSource = from dimos in dc.Dimois
                                            where dimos.Nomos == this.DropNomos.SelectedValue
                                            orderby dimos.Onoma
                                            select new { name = dimos.Onoma };
            this.DropDownList1.DataTextField = "name";
            this.DropDownList1.DataValueField = "name";
            this.DropDownList1.DataBind();
            this.DropDownList1.Items.Insert(0, new ListItem("Όλοι οι δήμοι", "fasa"));
        }
        protected void DropNomos_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList1_Populate();
        }
        protected void Button1_Click1(object sender, EventArgs e)
        {
            populate();
        }
    }
}